package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itcast.constant.MessageConstant;
import com.itcast.constant.RedisMessageConstant;
import com.itcast.entity.Result;
import com.itcast.pojo.Order;
import com.itcast.pojo.Setmeal;
import com.itcast.service.OrderService;
import com.itcast.service.SetmealService;
import com.itcast.untils.SMSUtils;
import com.itcast.untils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderService orderService;
    /**
     * 查询所有套餐
     * @return
     */
    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> setmeals;
        try {
            setmeals = setmealService.getSetmeal();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeals);
    }

    /**
     * 单个套餐详情展示页
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(@RequestParam("id") Integer id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }
    /**
     * 验证码的获取
     */
    @RequestMapping("/sendTelephone")
    public Result sendTelephone(@RequestParam("telephone") String telephone){
        //生成短信验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        System.out.println(code);
        //调用短信发送的工具类
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            //验证码发送失败
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //将生成的验证码存储在jedis中
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,10*60,code.toString());
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     * 进行预约者的添加
     * @param map
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Map map){
        String telephone = (String)map.get("telephone");
        //从jedis中获取验证码
        String s1 = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        String validateCode = (String)map.get("validateCode");
        if(s1 != null && validateCode != null && s1.equals(validateCode)){
            //加入验证方式
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            Result result = orderService.addUser(map);
            if (result.isFlag()){
                String orderDate = (String)map.get("orderDate");
                //预约成功发送短信通知
                try {
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,orderDate);
                } catch (ClientException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }else{
            return null;
        }
    }
}
